package cn.mastor.order.service.impl;

import cn.mastor.basic.config.BussinessException;
import cn.mastor.order.domain.OrderPetAcquisition;
import cn.mastor.order.dto.OrderConfirmDto;
import cn.mastor.order.mapper.OrderPetAcquisitionMapper;
import cn.mastor.order.service.IOrderPetAcquisitionService;
import cn.mastor.basic.service.impl.BaseServiceImpl;
import cn.mastor.pet.domain.Pet;
import cn.mastor.pet.domain.SearchMasterMsg;
import cn.mastor.pet.service.IPetService;
import cn.mastor.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-01
 */
@Service

public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IPetService petService;
    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderConfirmDto.getId());
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }

        // 3.查询寻主信息，修改状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);

        // 4.保存宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(orderConfirmDto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        System.out.println(pet);
        if (null!=pet) {
            petService.add(pet);
        }
        // 2.修改订单状态为完成
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    @Override
    @Transactional
    public void cancel(Long orderId) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 2.修改订单状态为取消
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 3.查询寻主信息，修改状态，清空shopId
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgService.update(searchMasterMsg);
    }
}
